namespace Pay.Core {
	using System;
    using System.Collections.Generic;
    using System.Configuration;
	using System.IO;
	using System.Runtime.InteropServices;
    using System.Text;
    using System.Web;

	public class IOHelper {
		public static void CopyFile(string fileFullPath, string destination, bool isDeleteSourceFile = false, string fileName = "") {
			if (string.IsNullOrWhiteSpace(fileFullPath)) {
				throw new ArgumentNullException("fileFullPath", "源文件全路径不能为空");
			}
			if (!File.Exists(fileFullPath)) {
				throw new FileNotFoundException("找不到源文件", fileFullPath);
			}
			if (!Directory.Exists(destination)) {
				throw new DirectoryNotFoundException("找不到目标目录 " + destination);
			}
			try {
				fileName = string.IsNullOrWhiteSpace(fileName) ? Path.GetFileName(fileFullPath) : fileName;
				File.Copy(fileFullPath, Path.Combine(destination, fileName), true);
				if (isDeleteSourceFile) {
					File.Delete(fileFullPath);
				}
			} catch (Exception) {
				throw;
			}
		}

		public static long GetDirectoryLength(string dirPath) {
			if (!Directory.Exists(dirPath)) {
				return 0L;
			}
			long num = 0L;
			DirectoryInfo info = new DirectoryInfo(dirPath);
			foreach (FileInfo info2 in info.GetFiles()) {
				num += info2.Length;
			}
			DirectoryInfo[] directories = info.GetDirectories();
			if (directories.Length > 0) {
				for (int i = 0; i < directories.Length; i++) {
					num += GetDirectoryLength(directories[i].FullName);
				}
			}
			return num;
		}

		public static string GetMapPath(params string[] path) {
			string fileName = "";
			if (HttpContext.Current != null) {
				fileName = Path.Combine(HttpContext.Current.Server.MapPath("~/Content"), Path.Combine(path));
			} else
				fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(path));
			string filePath = Path.GetDirectoryName(fileName);
			if (!Directory.Exists(filePath))
				Directory.CreateDirectory(filePath);
			return fileName;
		}

        public static IEnumerable<string> OpenCsv(string path) {
            if (HttpContext.Current != null) {
                path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content"), Path.Combine(path));
            } else
                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(path));
            var rows = File.ReadLines(path);
            return rows;
        }

        public static void SaveCsv(string path, string content) {
            if (string.IsNullOrEmpty(path))
                throw new Exception("path is not valid");
            if (string.IsNullOrEmpty(content))
                return;
            if (HttpContext.Current != null) {
                path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content"), Path.Combine(path));
            } else
                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(path));
            try {
                using (var fs = File.Open(path, FileMode.Append, FileAccess.Write)) {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) {
                        sw.WriteLine(content);
                    }
                }
            } catch (Exception ex) {
                var log = IocHelper.Get<ILogHelper>("info");
                log.Info(ex.Message);
            }
        }

    }
}

